package com.hangwei.paperless.view;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hangwei.paperless.R;
import com.hangwei.paperless.utils.KeyValue;
import com.twelve.all.utils.NotFastClickListener;
import com.twelve.all.utils.Tools;

/**
 * Created by 张龙臣 on 2020/11/20.
 */

public abstract class SealHelper extends BaseView<Context, SealHelper.SealBean> {


    public SealHelper(Context activity) {
        super(activity);
    }

    @Override
    protected View initView() {
        return LayoutInflater.from(mActivity).inflate(R.layout.helper_seal, null);
    }

    @Override
    public void bindData(SealBean sealBean) {
        LinearLayout llContent = mItemView.findViewById(R.id.llContent);
        ImageView ivSealIcon = mItemView.findViewById(R.id.ivSealIcon);
        TextView tvSealNum = mItemView.findViewById(R.id.tvSealNum);
        TextView rangeText = mItemView.findViewById(R.id.rangeText);
        TextView tvSealDate = mItemView.findViewById(R.id.tvSealDate);
        TextView tvSealName = mItemView.findViewById(R.id.tvSealName);
        ImageView ivSeal = mItemView.findViewById(R.id.ivSeal);
        ImageView ivNa = mItemView.findViewById(R.id.ivNa);
        RelativeLayout rlSeal = mItemView.findViewById(R.id.rlSeal);
        ivNa.setVisibility(sealBean.canNa ? View.VISIBLE : View.GONE);
        if (TextUtils.isEmpty(sealBean.stampId)) {
            if (sealBean.isNA) {//能盖章
                mItemView.setBackgroundColor(Color.TRANSPARENT);
                if (TextUtils.isEmpty(sealBean.id)) {
                    rlSeal.setVisibility(View.GONE);
                    tvSealDate.setVisibility(View.GONE);
                    if (sealBean.canSeal) {
                        ivSeal.setImageResource(R.drawable.seal);
                        ivSeal.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                onSeal();
                            }
                        });
                        llContent.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
//                                onSeal();
                            }
                        });
//                        ivNa.setVisibility(sealBean.canNa?View.VISIBLE:View.GONE);
                    } else {
                        ivSeal.setImageResource(R.drawable.seal_un);
                        ivSeal.setOnClickListener(null);
                        llContent.setOnClickListener(null);
//                        ivNa.setVisibility(View.GONE);
                    }
                } else {
                    showSeal(sealBean, ivSealIcon, tvSealNum, rangeText, tvSealDate, tvSealName, ivSeal, rlSeal);
//                    ivNa.setVisibility(View.GONE);
                    ivSeal.setVisibility(View.GONE);
                }
            } else {//什么也不显示，改变背景颜色
                mItemView.setBackgroundColor(mActivity.getResources().getColor(R.color.lightGray));
                rlSeal.setVisibility(View.GONE);
                tvSealDate.setVisibility(View.GONE);
                ivSeal.setVisibility(View.GONE);
                tvSealName.setVisibility(View.GONE);
                ivNa.setVisibility(View.GONE);
            }
        } else {
            mItemView.setBackgroundColor(Color.TRANSPARENT);
            showSeal(sealBean, ivSealIcon, tvSealNum, rangeText, tvSealDate, tvSealName, ivSeal, rlSeal);
            ivNa.setVisibility(View.GONE);
        }
        ivNa.setOnClickListener(new NotFastClickListener() {
            @Override
            protected void isNotFastClick(View v) {
                onNa();
            }
        });
//        if (!sealBean.canSeal){
//            ivNa.setVisibility(View.GONE);
//        }
//        if (!sealBean.canNa){
//            ivNa.setVisibility(View.GONE);
//        }

    }

    public void onNa() {

    }

    private void showSeal(SealBean sealBean, ImageView ivSealIcon, TextView tvSealNum, TextView rangeText, TextView tvSealDate, TextView tvSealName, ImageView ivSeal, RelativeLayout rlSeal) {
        ivSeal.setVisibility(View.GONE);
        rlSeal.setVisibility(View.VISIBLE);
        tvSealDate.setVisibility(View.VISIBLE);
        tvSealName.setVisibility(View.VISIBLE);
        int sealType = KeyValue.getSealType(Tools.checkedString(sealBean.stampTypeCode));
        int sealPic = KeyValue.getSealPic(sealBean.stampTypeCode);
        if (sealPic != 0 && sealPic != -1)
            ivSealIcon.setImageResource(sealPic);
        tvSealDate.setText(Tools.checkedString(sealBean.date));
        tvSealName.setText(Tools.checkedString(sealBean.stampName));
        if (sealType == 0) {//红色圆形
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tvSealNum.getLayoutParams();
            layoutParams.bottomMargin = Tools.dp2px(5);
            tvSealNum.setLayoutParams(layoutParams);
            tvSealNum.setVisibility(View.VISIBLE);
            tvSealNum.setText(Tools.checkedString(sealBean.stampCode));
            tvSealNum.setTextColor(mActivity.getResources().getColor(R.color.red_1));
            rangeText.setVisibility(View.GONE);
        } else if (sealType == 1) {//蓝色六边形
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tvSealNum.getLayoutParams();
            layoutParams.bottomMargin = Tools.dp2px(5);
            tvSealNum.setLayoutParams(layoutParams);
            tvSealNum.setVisibility(View.VISIBLE);
            tvSealNum.setText(Tools.checkedString(sealBean.stampCode));
            tvSealNum.setTextColor(mActivity.getResources().getColor(R.color.blue1));
            rangeText.setVisibility(View.GONE);
        } else if (sealType == 2) {//蓝色三角形
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tvSealNum.getLayoutParams();
            layoutParams.bottomMargin = Tools.dp2px(1);
            tvSealNum.setLayoutParams(layoutParams);
            tvSealNum.setVisibility(View.VISIBLE);
            tvSealNum.setText(Tools.checkedString(sealBean.stampCode));
            tvSealNum.setTextColor(mActivity.getResources().getColor(R.color.blue1));
            rangeText.setVisibility(View.GONE);
        } else {//红色矩形
            tvSealNum.setVisibility(View.GONE);
            rangeText.setText(Tools.checkedString(sealBean.stampCode));
            rangeText.setTextColor(mActivity.getResources().getColor(R.color.red_1));
            rangeText.setVisibility(View.VISIBLE);
        }
    }

    protected abstract void onSeal();

    public static class SealBean {
        public SealBean(boolean isNA, String stampId, String id, String stampTypeCode, String stampCode, String date, String stampName, boolean canSeal) {
            this.isNA = isNA;
            this.stampId = stampId;
            this.id = id;
            this.stampTypeCode = stampTypeCode;
            this.stampCode = stampCode;
            this.date = date;
            this.stampName = stampName;
            this.canSeal = canSeal;
        }

        public SealBean(boolean isNA, String stampId, String id, String stampTypeCode, String stampCode, String date, String stampName, boolean canSeal, boolean canNa) {
            this.isNA = isNA;
            this.stampId = stampId;
            this.id = id;
            this.stampTypeCode = stampTypeCode;
            this.stampCode = stampCode;
            this.date = date;
            this.stampName = stampName;
            this.canSeal = canSeal;
            this.canNa = canNa;
        }

        public boolean isNA; //false 表示什么也不显示
        public boolean canSeal;
        public boolean canNa = false;
        public String stampId;
        public String id;
        public String stampTypeCode;
        public String stampCode;
        public String date;
        public String stampName;
    }
}
